package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Bebida;
import services.BebidaService;

@FacesConverter("bebidaConverter")
public class BebidaConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
           BebidaService service = (BebidaService) fc.getExternalContext().getApplicationMap().get("bebidaService");
            Iterator<Bebida> i=service.getListaBebida().iterator();
            while (i.hasNext()) {
				Bebida b = (Bebida) i.next();
				if(b.getIdBebida() == Integer.parseInt(value))
					return b;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((Bebida) object).getIdBebida());
        }
        else {
            return null;
        }
    }  
}
